package com.persicadev.shamsidatetimepicker;

import java.util.Calendar;
import com.sahandrc.calendar.PersianCalendar;
import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.OnWheelClickedListener;
import kankan.wheel.widget.WheelView;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

/**
 * 
 * @author Hadi Alipoor
 *
 */
public class ShamsiControl extends LinearLayout {
	WheelView wvYear;
	WheelView wvMonth;
	WheelView wvDay;

	int curYear ;
	int curMonth;
	int curDay;
	Context context;
	ShamsiControl thiss;
	
	//List<OnDateChangedListener> changinglistener = new LinkedList<OnDateChangedListener>();
	OnDateChangedListener dateChangeListener;
	OnShamsiControlClickListener clickListener;
	
	public ShamsiControl(Context context) {
		super(context);
		this.context=context;
	}

	public ShamsiControl(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context=context;
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		((Activity)getContext()).getLayoutInflater().inflate(R.layout.shamsi_picker_control, this);
		Initialize();
	}

	public void Initialize(){
		thiss=this;
		//--- Wheel Initialize ---
		Calendar calendar = Calendar.getInstance();
		PersianCalendar persianCalendar = new PersianCalendar();
		persianCalendar.setGregorianChange(calendar.getTime());

		curYear = persianCalendar.getPersianYear();
		curMonth=persianCalendar.getPersianMonth();
		curDay=persianCalendar.getPersianDay();

		wvYear =(WheelView)findViewById(R.id.wheelYear);
		wvMonth=(WheelView)findViewById(R.id.wheelMonth);
		wvDay=(WheelView)findViewById(R.id.wheelDay);

		wvYear.setViewAdapter(new CustomeNumericAdapter(context, curYear-1, curYear+1,1));

		wvMonth.setVisibleItems(12);


		wvYear.addChangingListener(new OnWheelChangedListener() {
			
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				updateDays(wvYear, wvMonth, wvDay);
				if (dateChangeListener != null) {
					PersianCalendar oldpersiancalendar = new PersianCalendar();
					PersianCalendar newpersiancalendar = new PersianCalendar();
					oldpersiancalendar.setPersianDate(curYear+ oldValue-1, wvMonth.getCurrentItem()+1, wvDay.getCurrentItem()+1);
					newpersiancalendar.setPersianDate(curYear+ newValue-1, wvMonth.getCurrentItem()+1, wvDay.getCurrentItem()+1);
					dateChangeListener.onChanged(thiss, oldpersiancalendar, newpersiancalendar);
				}
			}
		});
		wvMonth.addChangingListener(new OnWheelChangedListener() {
			
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				updateDays(wvYear, wvMonth, wvDay);
				if (dateChangeListener != null) {
					PersianCalendar oldpersiancalendar = new PersianCalendar();
					PersianCalendar newpersiancalendar = new PersianCalendar();
					oldpersiancalendar.setPersianDate(curYear+ wvYear.getCurrentItem()-1, oldValue+1, wvDay.getCurrentItem()+1);
					newpersiancalendar.setPersianDate(curYear+ wvYear.getCurrentItem()-1, newValue+1, wvDay.getCurrentItem()+1);
					dateChangeListener.onChanged(thiss, oldpersiancalendar, newpersiancalendar);
				}
			}
		});
		wvDay.addChangingListener(new OnWheelChangedListener() {
			
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				updateDays(wvYear, wvMonth, wvDay);
				if (dateChangeListener != null) {
					PersianCalendar oldpersiancalendar = new PersianCalendar();
					PersianCalendar newpersiancalendar = new PersianCalendar();
					oldpersiancalendar.setPersianDate(curYear+ wvYear.getCurrentItem()-1, wvMonth.getCurrentItem()+1, oldValue+1);
					newpersiancalendar.setPersianDate(curYear+ wvYear.getCurrentItem()-1, wvMonth.getCurrentItem()+1, newValue+1);
					dateChangeListener.onChanged(thiss, oldpersiancalendar, newpersiancalendar);
				}
			}
		});
		
		OnWheelClickedListener wheelClickedListener = new OnWheelClickedListener() {
			
			public void onItemClicked(WheelView wheel, int itemIndex) {
				clickListener.OnClick(thiss, wheel);
			}
		};
		wvYear.addClickingListener(wheelClickedListener);
		wvMonth.addClickingListener(wheelClickedListener);;
		wvDay.addClickingListener(wheelClickedListener);
		
		wvMonth.setViewAdapter(new MonthAdapter(context));
		wvDay.setViewAdapter(new CustomeNumericAdapter(context, 1, 31,1));

		wvYear.setCurrentItem(1);
		wvMonth.setCurrentItem(curMonth-1);
		wvDay.setCurrentItem(curDay-1);

		
	}

	private void updateDays(WheelView year, WheelView month, WheelView day) {

		int monthItem = month.getCurrentItem();
		if (monthItem>=0 && monthItem<=5) {
			day.setViewAdapter(new CustomeNumericAdapter(context, 1, 31,1));
		}
		else if (monthItem>=6) {
			day.setViewAdapter(new CustomeNumericAdapter(context, 1, 30,1));
		}

	}

	
	public PersianCalendar getPersianCalendar()
	{
		PersianCalendar persiancalendar = new PersianCalendar();
		persiancalendar.setPersianDate(curYear+ wvYear.getCurrentItem()-1, wvMonth.getCurrentItem()+1, wvDay.getCurrentItem()+1);
		return persiancalendar;
	}
	
	public void setOnDateChangedListener(OnDateChangedListener l)
	{
		dateChangeListener=l;
	}
	
	public void setOnShamsiControlClickListener(OnShamsiControlClickListener l)
	{
		clickListener=l;
	}
	
	
	
	
	

}
